package login.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.db.DataSourceManager;
import login.bean.AssetBundleBean;

public class AssetBundleDao {

	private static Logger logger = LoggerFactory.getLogger(AssetBundleDao.class);

	private DataSource dataSource = DataSourceManager.getInstance().getDataSource();

	static class SingletonHolder {
		static AssetBundleDao instance = new AssetBundleDao();
	}

	public static AssetBundleDao getInstance() {
		return SingletonHolder.instance;
	}

	public List<AssetBundleBean> getServerList() {
		List<AssetBundleBean> ret = null;
		ResultSetHandler<List<AssetBundleBean>> handler = new ResultSetHandler<List<AssetBundleBean>>() {
			@Override
			public List<AssetBundleBean> handle(ResultSet rs) throws SQLException {
				List<AssetBundleBean> userBeanList = new ArrayList<AssetBundleBean>();
				while (rs.next()) {
					AssetBundleBean retBean = new AssetBundleBean();
					retBean.setId(rs.getInt("id"));
					retBean.setType(rs.getInt("type"));
					retBean.setName(rs.getString("name"));
					retBean.setMd5(rs.getString("md5"));
					userBeanList.add(retBean);
				}
				return userBeanList;
			}
		};
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			ret = runner.query("SELECT * from asset_bundle_download order by id asc", handler);
		} catch (SQLException e) {
			logger.error("", e);
			return ret;
		}
		return ret;
	}

}
